﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WineStore.DAL;
using WineStore.Models;

namespace WineStore.Controllers
{
    public class CheckoutController : Controller
    {
        WineDbContext storeDB = new WineDbContext();
        //
        // GET: /Checkout/AddressAndPayment
        public ActionResult AddressAndPayment(string identityName)
        {
            SetCookie();
            ViewBag.Logo = null;
            ViewBag.Categories = null;
            Customer customer = null;
            ViewBag.FullName = null;
            ViewBag.Address = null;
            ViewBag.Phone = null;
            ViewBag.Email = null;
            ViewBag.CustomerId = null;
            using (var db = new WineDbContext())
            {
                ViewBag.Logo = db.Logo.ToList();
                ViewBag.Categories = db.Categories.ToList();

                if (identityName == null)
                {
                    return RedirectToAction("LogonCheckout", "Home");
                }
                else
                {
                    customer = db.Customers.Where(c => c.UserName == identityName).First();
                    ViewBag.FullName = customer.FullName;
                    ViewBag.Address = customer.Address;
                    ViewBag.Phone = customer.Phone;
                    ViewBag.Email = customer.Email;
                    ViewBag.CustomerId = customer.CustomerId;
                }
            }
            return View();
        }
        //
        // POST: /Checkout/AddressAndPayment
        [HttpPost]
        public ActionResult AddressAndPayment(FormCollection values, int customerId)
        {
            SetCookie();
            ViewBag.Logo = null;
            ViewBag.Categories = null;
            using (var db = new WineDbContext())
            {
                ViewBag.Logo = db.Logo.ToList();
                ViewBag.Categories = db.Categories.ToList();
            }
            var order = new Order();
            TryUpdateModel(order);
            try
            {
                order.OrderDate = DateTime.Now;
                order.CustomerId = customerId;
                order.Status = 1;
                //Save Order
                storeDB.Orders.Add(order);
                storeDB.SaveChanges();
                //Process the order
                var cart = ShoppingCart.GetCart(this.HttpContext);
                cart.CreateOrder(order);
                return RedirectToAction("Complete",
                    new { id = order.OrderId });
            }
            catch
            {
                //Invalid - redisplay with errors
                return View(order);
            }
        }
        //
        // GET: /Checkout/Complete
        public ActionResult Complete(int id)
        {
            SetCookie();
            ViewBag.Logo = null;
            ViewBag.Categories = null;
            using (var db = new WineDbContext())
            {
                ViewBag.Logo = db.Logo.ToList();
                ViewBag.Categories = db.Categories.ToList();
            }
            // Validate customer owns this order
            bool isValid = storeDB.Orders.Any(
                o => o.OrderId == id);
            if (isValid)
            {
                return View(id);
            }
            else
            {
                return View("Error");
            }
        }

        // GET: /Home/ChangeCulture/
        public ActionResult ChangeCulture(string lang, string returnUrl)
        {
            HttpCookie cookies = Request.Cookies.Get("language");
            if (cookies == null)
            {
                cookies = new HttpCookie("language");
                cookies.Value = lang;
            }
            else
            {
                cookies.Value = lang;
            }
            cookies.Expires = DateTime.Now.AddDays(30);
            Response.Cookies.Add(cookies);
            return Redirect(returnUrl);
        }

        // SetCookie
        private void SetCookie()
        {
            HttpCookie cookie = Request.Cookies.Get("language");
            if (cookie == null)
            {
                cookie = new HttpCookie("language");
                Response.Cookies["language"].Value = "EN";
                Response.Cookies.Add(cookie);
            }
        }
    }
}
